﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MimallAPI.Dal;
using MimallAPI.Model;


namespace MimallAPI.Dal
{
    public class GoodsDal: IGoods
    {

       
        //上下文注入
        private readonly MiDBcontext db;


        public GoodsDal(MiDBcontext miDBcontext)
        {
            db = miDBcontext;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public UserInfo Login(UserInfo userInfo)
        {
           return db.UserInfo.Where(p => p.UserName == userInfo.UserName && p.UserPwd == userInfo.UserPwd).FirstOrDefault();
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public int registered(UserInfo u)
        {
            db.UserInfo.Add(u);
            return db.SaveChanges();
        }
        /// <summary>
        ///  商品列表分页显示
        /// </summary>
        /// <param name="totalcount"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public List<ViewGood> GetGood( out int totalcount,int pageindex,int pagesize)
        {
            
            var query = db.GoodsInfo.ToList();
            totalcount = query.Count();
            var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize).Select(u => new ViewGood
            {
                GoodId=u.GoodId,
                GoodName=u.GoodName,
                GoodPrice=u.GoodPrice,
                GoodArea=u.GoodArea,
                GoodBanben=u.GoodBanben,
                GoodColor=u.GoodColor,
                GoodImg= "http://localhost:13443/Img/" + u.GoodImg,
                GoodCount=u.GoodCount
            }).ToList();
            return list;
        }
        /// <summary>
        /// 商品列表展示
        /// </summary>
        /// <param name="GoodId"></param>
        /// <returns></returns>
        public ViewGood EditGood(int GoodId)
        {
           var list= db.GoodsInfo.Where(p => p.GoodId == GoodId).Select(u => new ViewGood
            {
               GoodId = u.GoodId,
               GoodName = u.GoodName,
               GoodPrice = u.GoodPrice,
               GoodArea = u.GoodArea,
               GoodBanben = u.GoodBanben,
               GoodColor = u.GoodColor,
               GoodImg = "http://localhost:13443/Img/" + u.GoodImg,
               GoodCount = u.GoodCount
           });
            return list.FirstOrDefault();
        }
        /// <summary>
        /// 添加到购物车
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public int AddShopCar(ShopCar s)
        {
            db.ShopCar.Add(s);
            return db.SaveChanges();
        }
        /// <summary>
        /// 购物车数据显示
        /// </summary>
        /// <returns></returns>
        public List<ViewShopCar> GetShopCar(int UserId)
        {
            var list = from a in db.ShopCar
                       join b in db.GoodsInfo on a.GoodId equals b.GoodId
                       join c in db.UserInfo on a.UserId equals c.UserId
                       where a.GoDelete==0 && c.UserId==UserId
                       select new ViewShopCar
                       {
                           CarId=a.CarId,
                           GoodId=b.GoodId,
                           UserId=c.UserId,
                           GoodName=b.GoodName,
                           GoodImg= "http://localhost:13443/Img/" + b.GoodImg,
                           GoodPrice=b.GoodPrice,
                           CarCount=a.CarCount,
                       };
            return list.ToList();
        }

        /// <summary>
        ///逻辑删除购物车数据
        /// </summary>
        /// <param name="CarId"></param>
        /// <returns></returns>
        public int DelShopcar(int CarId)
        {
            var list = db.ShopCar.FirstOrDefault(p => p.CarId == CarId);
            if(list!=null)
            {
                list.GoDelete = 1;
            }
            return db.SaveChanges();
        }
        /// <summary>
        /// 更改数量
        /// </summary>
        /// <param name="CarId"></param>
        /// <param name="CarCount"></param>
        /// <returns></returns>
        public int editcount(int CarId,int CarCount)
        {
            var list = db.ShopCar.FirstOrDefault(u => u.CarId == CarId);
            list.CarCount = CarCount;
            return db.SaveChanges();
        }

        /// <summary>
        /// 地址联动
        /// </summary>
        /// <param name="Fid"></param>
        /// <returns></returns>
        public List<Site> Getsite(int Fid)
        {
            return db.Site.Where(p => p.Fid == Fid).ToList();
        }
        /// <summary>
        /// 收货信息显示
        /// </summary>
        /// <returns></returns>
        public List<ViewRece> GetRece()
        {
            var list = from a in db.ReceInfo
                       join b in db.Site on a.sheng equals b.Sid
                       join c in db.Site on a.shi equals c.Sid
                       join d in db.Site on a.xian equals d.Sid
                       select new ViewRece
                       {
                           ReceId=a.ReceId,
                           ReceName=a.ReceName,
                           RecePhone=a.RecePhone,
                           ReceArea=a.ReceArea,
                           ReceEmail=a.ReceEmail,
                           sheng =a.sheng,
                           shi=a.shi,
                           xian=a.xian,
                           shengname=b.Sname,
                           shiname=c.Sname,
                           xianname=d.Sname
                       };
            return list.ToList();
        }
        /// <summary>
        /// 添加收货信息
        /// </summary>
        /// <param name="r"></param>
        /// <returns></returns>
        public int addRece(ReceInfo r)
        {
            db.ReceInfo.Add(r);
            return db.SaveChanges();
        }
        /// <summary>
        /// 删除收货信息
        /// </summary>
        /// <param name="ReceId"></param>
        /// <returns></returns>
        public int DelRece(int ReceId)
        {
            var list = db.ReceInfo.FirstOrDefault(p=>p.ReceId==ReceId); 
            db.ReceInfo.Remove(list);
            return db.SaveChanges();
        }
       /// <summary>
       /// 订单列表添加
       /// </summary>
       /// <param name="adi"></param>
       /// <returns></returns>
        public int AddInde(AddIndent adi)
        {
            //雪花算法生成
            var worker = IdWorkerSing._idWorker;
            long id = worker.NextId();
            adi.IndentNumber = id.ToString();

            db.Add(adi);

            foreach (var item in adi.Indents)
            {
                item.IndentNumber = id.ToString();
                 db.Indents.Add(item);
            }
            return db.SaveChanges();
        }

        public List<ViewAddindent> GetIndent()
        {
            var list = from a in db.Indents
                       join b in db.GoodsInfo on a.GoodId equals b.GoodId
                       join c in db.addIndent on a.IndentNumber equals c.IndentNumber
                       join d in db.ReceInfo on c.ReceId equals d.ReceId
                       select new ViewAddindent
                       {
                           GoodName=b.GoodName,
                           GoodImg= "http://localhost:13443/Img/" + b.GoodImg,
                           CreateTime=c.CreateTime,
                           IndentGooder=c.IndentGooder,
                           IndentNumber=a.IndentNumber,
                           IndentSum=c.IndentSum,
                           GoodId=a.GoodId,
                           InId=a.InId,
                           ReceId=c.ReceId,
                           ReceName=d.ReceName
                       };
                    
            return list.ToList();
        }

        public List<ViewAddindent> DDTJ()
        {
            var list = from a in db.Indents
                       join b in db.GoodsInfo on a.GoodId equals b.GoodId
                       join c in db.addIndent on a.IndentNumber equals c.IndentNumber
                       join d in db.ReceInfo on c.ReceId equals d.ReceId
                       where c.Aid==3
                       select new ViewAddindent 
                       {
                           GoodName = b.GoodName,
                           GoodImg = "http://localhost:13443/Img/" + b.GoodImg,
                           CreateTime = c.CreateTime,
                           IndentGooder = c.IndentGooder,
                           IndentNumber = a.IndentNumber,
                           IndentSum = c.IndentSum,
                           GoodId = a.GoodId,
                           InId = a.InId,
                           ReceId = c.ReceId,
                           ReceName = d.ReceName
                       };

            return list.ToList();
        }
    }
}
